import { Component } from '@angular/core';
import { NavController, AlertController } from 'ionic-angular';
import { AuthService } from '../../providers/auth-service';
import { DictionaryService } from '../../providers/dictionary-service';

 
@Component({
  selector: 'page-register',
  templateUrl: 'register.html'
})
export class Register {
  registerCredentials = {
    user: {
      email: '', 
      username: '',
      idcard: '',
      gender: '',
      phone: '',
      address: '',
      password: ''
    },
    factory: {
      name: '',
      address: '',
      create_time: '',
      regoistered_capital:'',
      legal_person: '',
      business_code: '',
      main_business: '',
      type: ''
    }
  };
  MainBusiness= []

  constructor(private nav: NavController, 
    private auth: AuthService,
    private alertCtrl: AlertController,
    private dict: DictionaryService) {
    this.MainBusiness =  dict.getByType('business')
  }
 
  public register() {
    this.auth.register(this.registerCredentials).then(success => {
      if (success) {
          this.showPopup("注册成功", "即将返回登录页面");
      } else {
        this.showPopup("Error", "");
      }
    },
    error => {
      this.showPopup("Error", error);
    });
  }
 
  showPopup(title, text) {
    let alert = this.alertCtrl.create({
      title: title,
      subTitle: text,
      buttons: [
       {
         text: 'OK',
         handler: data => {
           this.nav.popToRoot();
         }
       }
     ]
    });
    alert.present();
  }
}
